﻿// (c) Copyright Michael Antonio.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved
using System;
using System.Linq;
using System.Xml.Linq;
using System.Xml;
using System.Collections.Generic;

namespace MichMan.Utilities.Requests
{
    public class AtomFeedItem
    {
        [ZPath("updated")]
        public DateTime Updated { get; set; }

        [ZPath("title")]
        public string Title { get; set; }

        [ZPath("id")]
        public string Id { get; set; }

        [ZPath("link")]
        public List<Link> Links { get; set; }
    }

    public class Link
    {
        [ZPath("@rel")]
        public string Relationship { get; set; }

        [ZPath("@href")]
        public string HRef { get; set; }
    }

    public class AtomFeed<TEntry> : AtomFeedItem where TEntry : AtomEntry
    {
        [ZPath("entry")]
        public List<TEntry> Entries { get; set; }
    }

    public class AtomEntry : AtomFeedItem
    {
    }

    public class AtomFeedRequest<TEntry> : ZPathDataRequest<AtomFeed<TEntry>> where TEntry : AtomEntry
    {
    }

    public class AtomEntryRequest<TEntry> : ZPathDataRequest<TEntry> where TEntry : AtomEntry, new()
    {
    }

    public class ZPathDataRequest<TResp> : DataRequest<TResp>
        where TResp : class, new()
    {
        protected virtual void ProcessResult(XElement elt)
        {
            Response = ZPathSerializer<TResp>.Load(elt);
        }

        protected override void ProcessResult(System.Xml.XmlReader reader)
        {
            if (ZPathSerializer.IsZPathSerializable(typeof(TResp)))
            {
                ProcessResult(XElement.Load(reader));
            }
            else
            {
                base.ProcessResult(reader);
            }
        }
    }
}